package com.supply.service.impl;

import com.supply.mapper.SalesBillMapper;
import com.supply.pojo.SalesBill;
import com.supply.pojo.SalesBillExample;
import com.supply.req.SalesBillReq;
import com.supply.service.SalesBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class SalesBillServiceImpl implements SalesBillService {
    @Autowired
    private SalesBillMapper salesBillMapper;

    @Override
    public List<SalesBill> queryByCondition(SalesBillReq salesBillReq) {
        SalesBillExample example = new SalesBillExample();
        SalesBillExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(salesBillReq.getTitle())){
            criteria.andTitleLike("%" + salesBillReq.getTitle() + "%");
        }
        if (!ObjectUtils.isEmpty(salesBillReq.getGoodsId())){
            criteria.andGoodsIdEqualTo(salesBillReq.getGoodsId());
        }
        return salesBillMapper.selectByExample(example);
    }

    @Override
    public int saveOrUpdate(SalesBill salesBill) {

        if (salesBill.getId() == null){
            return salesBillMapper.insert(salesBill);
        }else {
            return salesBillMapper.updateByPrimaryKey(salesBill);
        }
    }

    @Override
    public int deleteById(Long id) {
        return salesBillMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SalesBill> queryAll() {

        return salesBillMapper.selectByExample(null);
    }

    @Override
    public SalesBill queryById(Long id) {
        return salesBillMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SalesBill> queryNotProcessed() {
        //还没被出库审批的销售单的状态为0
        SalesBillExample example = new SalesBillExample();
        example.createCriteria().andStateEqualTo(0);
        return salesBillMapper.selectByExample(example);
    }
}
